const data = require('./src/mock')
const py = require('js-pinyin')
module.exports = {
    devServer: {
        proxy: {
            '/api': {
                target: 'http://127.0.0.1:7001'
            }
        },
        before(app) {
            app.get('/api/lists', (req, res) => {
                //处理数据 ==> 筛选市、县数据
                let list = data.filter(v => v.level > 1 && v.name !== '市辖区')
                //console.log(py.getFullChars(list[0].name));
                let cityObj = {}, //保存城市的对象
                    sortObj = {}
                list.map(v => {
                    //获取当前数据的name的拼音的首字母
                    let firstWord = py.getFullChars(v.name).slice(0, 1)

                    if (cityObj[firstWord]) {
                        cityObj[firstWord].push(v)
                    } else {
                        cityObj[firstWord] = [v]
                    }
                })
                //Object.keys: 返回由对象的键组成的新数组
                let wordList = Object.keys(cityObj).sort();
                wordList.map(v => {
                    sortObj[v] = cityObj[v]
                    return v;
                })

                res.send({
                    code: 1,
                    sortObj
                })
            })
        }
    }
}

